package cycling.taxi.com.ui.activity;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;

import butterknife.ButterKnife;
import butterknife.InjectView;
import cycling.taxi.com.IConstants;
import cycling.taxi.com.R;
import cycling.taxi.com.person.ExitApplication;
import cycling.taxi.com.request.Config;
import cycling.taxi.com.request.RM;
import cycling.taxi.com.request.RequestBean;
import cycling.taxi.com.request.RequestCallback;
import cycling.taxi.com.request.RequestParams;
import cycling.taxi.com.request.RequestUtil;
import cycling.taxi.com.utils.MD5Util;
import cycling.taxi.com.utils.PhoneUtils;
import cycling.taxi.com.utils.ToastUtil;
import cycling.taxi.com.utils.Urls;

public class ChangePasswordActivity extends AppCompatActivity implements View.OnClickListener{
    @InjectView(R.id.back)
    ImageView back;
    @InjectView(R.id.title_text)
    TextView title_text;
    @InjectView(R.id.submit)
    Button submit;
    @InjectView(R.id.pwd1)
    TextView pwd1;
    @InjectView(R.id.pwd2)
    TextView pwd2;
    //volley请求队列
    private RequestQueue queue;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_change_password);
        //控件注入
        ButterKnife.inject(this);
        queue= Volley.newRequestQueue(this);
        //添加到Activity容器
        ExitApplication.getInstance().addActivity(this);
        InitView();
        InitListener();
    }
    private void InitView() {
        title_text.setText("设置密码");
    }
    private void InitListener(){
        submit.setOnClickListener(this);
        back.setOnClickListener(this);
    }
    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.back:
                finish();
                break;
            case R.id.submit:
                if(TextUtils.isEmpty(getPassword(pwd1))){
                    ToastUtil.getInstance().toast("密码不能为空");
                    break;
                }

                if(getPassword(pwd1).length()<6||getPassword(pwd2).length()<6){
                    ToastUtil.getInstance().toast("密码长度不能少于6位数");
                    break;
                }
                if(!getPassword(pwd1).equals(getPassword(pwd2))){
                    ToastUtil.getInstance().toast("两次密码不同");
                    break;
                }
                changePassword();
                break;
        }
    }
    //密码校验
    private String getPassword(TextView pwd) {
        return pwd.getText().toString().trim();
    }
    //密码修改
    private void changePassword(){
        String phone =getIntent().getStringExtra("phone");
        String path= Urls.getChangePassWord()+"?phone="+phone.trim()+"&password="+MD5Util.MD5Encode(getPassword(pwd1).trim());
        StringRequest request = new StringRequest(path, new Response.Listener<String>() {
            @Override
            public void onResponse(String s) {
                if(s.equals("1")) {
                    ToastUtil.getInstance().toast("修改成功");
                    finish();
                }
                else {
                    ToastUtil.getInstance().toast("修改失败");
                }

            }
        },null);
        queue.add(request);
    }

}
